package com.strawcom.charmring.ui;

import org.kymjs.kjframe.utils.SystemTool;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Request.Method;
import com.strawcom.charmring.BaseActivity;
import com.strawcom.charmring.C;
import com.strawcom.charmring.R;
import com.strawcom.charmring.bean.http.BaseResponse;
import com.strawcom.charmring.bean.http.GetContactInfoRequest;
import com.strawcom.charmring.bean.http.GetContactInfoResponse;
import com.strawcom.charmring.util.HttpUtil;
import com.strawcom.charmring.util.HttpUtil.ResponeListener;
import com.strawcom.charmring.view.TitleView;

public class ContactInfoActivity extends BaseActivity implements OnClickListener{

	private TitleView titleView;
	private TextView tvNumber,tvName,tvState,tvNick;
	private static final String NAME = "name", NUMBER = "number";
	private ContentValues contactInfo;
	private Button btnDelete,btnInvitation;
	private String[] states;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_contact_info);
		super.onCreate(savedInstanceState);
		initViews();
	}
	
	private void initViews(){
		states=getResources().getStringArray(R.array.regstate);
		contactInfo=getIntent().getParcelableExtra("contactInfo");
		
		titleView = (TitleView) findViewById(R.id.title_view);
		titleView.setTitleText(R.string.title_contact_info);
		
		tvNumber=(TextView) findViewById(R.id.tv_number);
		tvName=(TextView) findViewById(R.id.tv_name);
		String phone = contactInfo.getAsString(NUMBER);
		phone = phone == null ? "":phone;
		// phone 有86头 去掉
//		if (phone.length() > 11) {
//			phone = phone.substring(phone.length()-11);
//		}
		tvNumber.setText(phone);
		tvName.setText(contactInfo.getAsString(NAME));
		tvState=(TextView) findViewById(R.id.tv_status);
		tvNick=(TextView) findViewById(R.id.tv_nick);
		
		btnDelete=(Button) findViewById(R.id.btn_delete);
		btnDelete.setOnClickListener(this);
		btnInvitation=(Button) findViewById(R.id.btn_invitation);
		btnInvitation.setOnClickListener(this);
		
		queryInfo();
	}

	private void queryInfo(){
		GetContactInfoRequest contactInfoRequest=new GetContactInfoRequest();
		contactInfoRequest.msgid=C.request_msgid_get_contact_info;
		String phone = contactInfo.getAsString(NUMBER);
		contactInfoRequest.ck=tvNumber.getText().toString();
		HttpUtil.requestJson(Method.GET, contactInfoRequest, new ResponeListener() {
			
			@Override
			public void onResponse(BaseResponse response,String resultStr) {
				GetContactInfoResponse infoResponse=HttpUtil.gson.fromJson(resultStr, GetContactInfoResponse.class);
				if(infoResponse.contactlist!=null&&infoResponse.contactlist.size()>0){
					tvState.setText(states[infoResponse.contactlist.get(0).regstate]);
					String nick=infoResponse.contactlist.get(0).regname;
					if(nick!=null&&!"".equals(nick)){
						tvNick.setText(nick);
					}else{
						tvNick.setText(R.string.unsetting);
					}
				}else{
					tvState.setText(states[0]);
					tvNick.setText(R.string.unsetting);
				}
			}
			
			@Override
			public void onErrorResponse(Exception error) {
				tvState.setText(states[0]);
				tvNick.setText(R.string.unsetting);
				if (SystemTool.checkNet(ContactInfoActivity.this)) {
					showToast(R.string.request_error);
				} else {
					showToast(R.string.net_error);
				}
			}
		});
	}
	
	@Override
	public void onClick(View view) {
		switch (view.getId()) {
		case R.id.btn_delete:
			setResult(RESULT_OK, getIntent());
			finish();
			break;
		case R.id.btn_invitation:
			sendSMS(this, getString(R.string.sms_msg),contactInfo.getAsString(NUMBER));
			break;
		default:
			break;
		}
	}
	
	public void sendSMS(Context cxt, String smsBody,String phone) {
        Uri smsToUri = Uri.parse("smsto:"+phone);
        Intent intent = new Intent(Intent.ACTION_SENDTO, smsToUri);
        intent.putExtra("sms_body", smsBody);
        cxt.startActivity(intent);
    }

}
